package com.module.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.module.dialog.adapter.ListAdapter;
import com.module.dialog.inter.DialogOnItemListener;
import com.module.dialog.inter.IViewData;
import com.module.dialog.utils.ListType;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 列表/单选/多选类型对话框
 * showd by dell on 2018/7/5
 */

public class ListDialog<T> extends BaseBuilder implements DialogOnItemListener {
    RecyclerView recyclerView; //列表控件
    ListParam mListParam; //列表参数

    public ListDialog(Context context) {
        super(context);
        initContentView();
    }

    /**
     * 初始化控件
     */
    @Override
    public void initContentView() {
        mListParam = new ListParam();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_list, null);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mListParam.adapter = new ListAdapter(mContext);
        mListParam.adapter.setOnItemListener(this);
        setContentView(view);
    }

    @Override
    public BaseBuilder getBuilder() {
        return this;
    }

    @Override
    protected void connectView() {
        mListParam.adapter.setListParam(mListParam);
        recyclerView.setAdapter(mListParam.adapter);
    }

    /**
     * 列表类型
     *
     * @param items
     * @param listener
     * @return
     */
    public ListDialog setItems(List<T> items, DialogOnItemListener listener) {
        mListParam.onItemListener = listener;
        mListParam.type = ListType.Normal;
        mListParam.items = items;
        return this;
    }

    /**
     * 单选类型1
     *
     * @param items
     * @return
     */
    public ListDialog setSingleChoiceItems(List<T> items, int selectItem, DialogOnItemListener listener) {
        mListParam.selectItem = selectItem;
        mListParam.onItemListener = listener;
        mListParam.type = ListType.Radio;
        mListParam.items = items;
        return this;
    }

    /**
     * 单选类型2
     *
     * @param items
     * @return
     */
    public ListDialog setSingleChoiceItems(List<T> items) {
        setSingleChoiceItems(items, -1, null);
        return this;
    }

    /**
     * 多选类型1
     *
     * @param items
     * @param checkItems
     * @param listener
     * @return
     */
    public ListDialog setMultiChoiceItems(List<T> items, boolean[] checkItems, DialogOnItemListener listener) {
        mListParam.onItemListener = listener;
        mListParam.type = ListType.CheckBox;
        mListParam.items = items;
        mListParam.checkItems = checkItems == null ? new boolean[items.size()] : checkItems;
        return this;
    }

    /**
     * 多选类型2
     *
     * @param items
     * @return
     */
    public ListDialog setMultiChoiceItems(List<T> items) {
        setMultiChoiceItems(items, null, null);
        return this;
    }

    /**
     * 点击按钮1，按钮2触发DialogCallBack事件
     * 回调时需要判断which是哪一个按钮
     *
     * @param listener
     * @return
     */
    public ListDialog setCallBack(DialogCallBack listener) {
        mListParam.callBackListener = listener;
        return this;
    }

    /**
     * 重写按钮点击事件
     */
    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (mListParam.callBackListener != null) {
            int tag = Integer.parseInt(v.getTag().toString());
            mListParam.callBackListener.callback(tag, getSelectItems(), getSelectValue());
        }
    }

    /**
     * 获取选中段position
     * 列表与单选默认返回[-1]
     * 多选默认返回[]
     *
     * @return
     */
    public int[] getSelectItems() {
        int[] selectItems = null;
        if (mListParam.type == ListType.Normal || mListParam.type == ListType.Radio) {
            selectItems = new int[]{mListParam.selectItem};
        } else if (mListParam.type == ListType.CheckBox) {
            List<Integer> posList = new ArrayList<>();
            for (int i = 0; i < mListParam.checkItems.length; i++) {
                if (mListParam.checkItems[i]) {
                    posList.add(i);
                }
            }
            selectItems = new int[posList.size()];
            for (int i = 0; i < posList.size(); i++) {
                selectItems[i] = posList.get(i);
            }
        }
        return selectItems;
    }

    /**
     * 获取选中的position对应value
     *
     * @return
     */
    public String getSelectValue() {
        String result = "";
        if (mListParam.type == ListType.Normal || mListParam.type == ListType.Radio) {
            if (mListParam.selectItem != -1) {
                result = mListParam.getText(mListParam.selectItem);
            }
        } else if (mListParam.type == ListType.CheckBox) {
            List<Integer> posList = new ArrayList<>();
            for (int i = 0; i < mListParam.checkItems.length; i++) {
                if (mListParam.checkItems[i]) {
                    posList.add(i);
                }
            }
            if (posList.size() > 0) {
                for (int i = 0; i < posList.size(); i++) {
                    result = result + mListParam.getText(posList.get(i)) + ",";
                }
                result = result.substring(0, result.lastIndexOf(","));
            }

        }
        return result;
    }

    @Override
    public void onItemClick(int position) {
        if (mListParam.type == ListType.Normal) {
            mListParam.selectItem = position;
            dialog.dismiss();
        } else if (mListParam.type == ListType.Radio) {
            mListParam.selectItem = position;
            mListParam.adapter.notifyDataSetChanged();
        } else if (mListParam.type == ListType.CheckBox) {
            mListParam.checkItems[position] = !mListParam.checkItems[position];
            mListParam.adapter.notifyDataSetChanged();
        }
        if (mListParam.onItemListener != null) {
            mListParam.onItemListener.onItemClick(position);
        }
    }

    /**
     * 列表参数
     */
    public class ListParam {
        public ListAdapter adapter;
        public ListType type = ListType.Normal;
        public List<T> items;
        public int selectItem = -1;
        public boolean[] checkItems;
        public DialogOnItemListener onItemListener; //点击item事件
        public DialogCallBack callBackListener; //点击确定/取消事件

        public String getText(int position) {
            Object object = items.get(position);
            if (object == null) {
                return "";
            } else if (object instanceof IViewData) {
                return ((IViewData) object).getText();
            }
            return object.toString();
        }
    }

    public interface DialogCallBack {
        void callback(int which, int[] positions, String result);
    }

}
